<?php

/**
 * 验证DEMO
 *
 * @author fuxiang <892576@gmail.com>
 *
 * $Id$
 */

use lib\util\Validation as Valida;

require_once('../../code/common.inc.php');

class Validation extends lib\core\Action
{
    public function doDefault()
    {
        //提交的demo数据
        //$_GET['gender'] = 4;
        $_POST['title'] = '中华人民共和国';
        //$_POST['body'] = "144";
        $_POST['selected'] = 11;
        $_POST['email'] = 'aaaaa@bbbb';
        $_POST['mobile'] = '12645678901';

        //实例化
        $vilida = new Valida();
        //加载规则
        $ret = $vilida->validate([
            'title' => 'post|required|string|clen:8|minclen:8',
            'body' => 'post|string|max:123|min:99',
            'gender' => 'get|int|required|range:1-2',
            'selected' => 'post|int|$>1|$<=10',
            'email' => 'post|email',
            'mobile' => 'post|phone'
        ]);
        //如果全部验证通过返回true，否则返回false;
        var_dump($ret);
        if ($ret) {
            //取验证通过的数据
            $data = $vilida->getValue();
            pre($data);
            //返回结果
            // <pre>
            //Array
            //(
            //    [title] => asdf
            //    [body] => 100
            //    [gender] => 1
            //    [selected] => 8
            //    [email] => aaaaa@bbbb.com
            //    [mobile] => 15645678901
            //)
        } else {
            //取错误信息
            $error = $vilida->getError();
            pre($error);
            //<pre>
            // Array
            // (
            //    [body] => 数值超过上限
            //    [gender] => 数据超出允许值范围
            //    [selected] => 数值大于限定
            //    [email] => Email格式不正确
            //    [mobile] => 手机号码填写不正确
            //)
            //</pre>
        }
    }
}

$app->run();